package com.sigmatrix.h5.web.dealer.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import com.sigmatrix.h5.entity.AdminArea;
import com.sigmatrix.h5.entity.DisTerminalShop;
import com.sigmatrix.h5.enums.CoordType;
import com.sigmatrix.h5.service.AdminAreaService;
import com.sigmatrix.h5.service.DisTerminalShopService;
import com.sigmatrix.h5.utils.CommonUtils;
import com.sigmatrix.h5.utils.SessionUtils;
import com.sigmatrix.h5.utils.SimpleRowMapper;
import com.sigmatrix.h5.utils.baidu.BaiduMapUtils;
import com.sigmatrix.h5.utils.baidu.po.LatLngLocation;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
/**
 * 
 *@ClassName: ShopLoactionCorrecter
 *@Description:对GPS有问题的终端店，重新定位， 仅用于调试
 *@author MH
 *@date 2018年3月2日 上午9:50:24
 */
@Api(tags="订正终端店地址")
@RestController
public class ShopLoactionCorrecter {
	
	@Autowired
	JdbcTemplate jdbcTemplate;
	@Autowired
	AdminAreaService areaService;
	@Autowired
	DisTerminalShopService shopService;
	@ApiOperation("对GPS解析异常的终端店,重新解析,仅用于开发阶段")
	@GetMapping("/dealer/correct")
	@Transactional
	public List<ShopPo> correct(){
		StringBuffer sql = new StringBuffer();
		sql.append("SELECT ");
		sql.append("a._id,a._name,a._province,b._name _province_name,a._city,c._name _city_name,a._city_sub_ordinate,d._name _city_sub_ordinate_name,a._address,a._lat,a._lng ");
		sql.append(" from _").append(SessionUtils.getEseCode()).append("_distribution_terminal_shop a");
		sql.append(" left join	_admin_area b  on b._id = a._province");
		sql.append(" left join _admin_area c on c._id = a._city");
		sql.append(" left join _admin_area d on d._id = a._city_sub_ordinate ");
		sql.append("where 1=1  and b._type != 1");
		List<ShopPo> list = jdbcTemplate.query(sql.toString(), SimpleRowMapper.createDefault(ShopPo.class));
		alter(list);
		//list.forEach(x ->System.out.println(JSONObject.toJSONString(x)));
		return list;
	}
	private void alter(List<ShopPo> list) {
		Map<Integer,AdminArea> map = new HashMap<>();
		List<AdminArea> areas = areaService.findByAllByCache();
		areas.forEach(x -> map.put(x.getId(), x));
		for (ShopPo shopPo : list) {
			shopPo.setCity(null);
			shopPo.setCityName(null);
			shopPo.setCitySubOrdinate(null);
			shopPo.setCitySubOrdinateName(null);
			shopPo.setProvince(null);
			shopPo.setProvinceName(null);
			LatLngLocation location = BaiduMapUtils.parseLatAndLng(shopPo.getLat(), shopPo.getLng(),CoordType.GCJ02LL);
			for (AdminArea area : areas) {
				int i =0;
				if(location.getResult().getAddressComponent().getCity().contains(area.getName())) {
					shopPo.setCity(area.getId());
					shopPo.setCityName(area.getName());
					shopPo.setProvince(area.getParent());
					AdminArea province = map.get(area.getParent());
					if(CommonUtils.isNotEmpty(province))shopPo.setProvinceName(province.getName());
					i ++;
				}
				if(location.getResult().getAddressComponent().getDistrict().contains(area.getName())) {
					shopPo.setCitySubOrdinate(area.getId());
					shopPo.setCitySubOrdinateName(location.getResult().getAddressComponent().getDistrict());
					i++;
				}
				if(i >= 2) break;
			}
			if(CommonUtils.isEmpty(shopPo.getCity()) && CommonUtils.isNotEmpty(shopPo.getCitySubOrdinate())) {
				AdminArea area = map.get(shopPo.getCitySubOrdinate());
				shopPo.setCity(area.getParent());
				AdminArea city = map.get(area.getParent());
				if(CommonUtils.isNotEmpty(city)) {
					shopPo.setCityName(city.getName());
					AdminArea province = map.get(city.getParent());
					if(CommonUtils.isNotEmpty(province))
						shopPo.setProvinceName(province.getName());
						shopPo.setProvince(province.getId());
				}
			}
			DisTerminalShop shop = new DisTerminalShop();
			shop.setId(shopPo.getId());
			shop.setProvince(shopPo.getProvince());
			shop.setCity(shopPo.getCity());
			shop.setCitySubOrdinate(shopPo.getCitySubOrdinate());
			shopService.updateSelective(shop);
		}
	}
	public static class ShopPo{
		private Integer id;
		private String name; 
		private Integer province;
		private String provinceName;
		private Integer city;
		private String cityName;
		private Integer citySubOrdinate;
		private String citySubOrdinateName;
		private String address;
		private String lat;
		private String lng;
		public String getName() {
			return name;
		}
		public void setName(String name) {
			this.name = name;
		}
		public Integer getProvince() {
			return province;
		}
		public void setProvince(Integer province) {
			this.province = province;
		}
		public String getProvinceName() {
			return provinceName;
		}
		public void setProvinceName(String provinceName) {
			this.provinceName = provinceName;
		}
		public Integer getCity() {
			return city;
		}
		public void setCity(Integer city) {
			this.city = city;
		}
		public String getCityName() {
			return cityName;
		}
		public void setCityName(String cityName) {
			this.cityName = cityName;
		}
		public Integer getCitySubOrdinate() {
			return citySubOrdinate;
		}
		public void setCitySubOrdinate(Integer citySubOrdinate) {
			this.citySubOrdinate = citySubOrdinate;
		}
		public String getCitySubOrdinateName() {
			return citySubOrdinateName;
		}
		public void setCitySubOrdinateName(String citySubOrdinateName) {
			this.citySubOrdinateName = citySubOrdinateName;
		}
		public String getLat() {
			return lat;
		}
		public void setLat(String lat) {
			this.lat = lat;
		}
		public String getLng() {
			return lng;
		}
		public void setLng(String lng) {
			this.lng = lng;
		}
		public String getAddress() {
			return address;
		}
		public void setAddress(String address) {
			this.address = address;
		}
		public Integer getId() {
			return id;
		}
		public void setId(Integer id) {
			this.id = id;
		}
	}
}
